using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class JobSeekers_jobapplied : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!IsPostBack)
            ShowData();

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ShowData();
        GridView1.PageIndex = e.NewPageIndex;
    }
    void ShowData()
    {
        try
        {
            ControlHelper cls = new ControlHelper("");
            cls.CommandText = "SPViewAppliedJob";
            cls.ParameterCollection = new string [1] {"@UserID"};
            cls.ValueCollection = new string[1] { 
            Convert.ToString(Session.Contents["jsuserid"])};
            cls.CommandType = CommandType.StoredProcedure;
            cls.InitProperties();
            cls.FillDataTableToGridView(this.GridView1, 10, 10, PagerButtons.NumericFirstLast);
            if(ShowDelete(this.GridView1))
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }
        catch(Exception ex)
        {
            Literal1.Text = ex.Message;
        }
    }
    bool ShowDelete(GridView gridView)
    {
        bool enableDelete = false;
        foreach (GridViewRow gridViewRow in gridView.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkJobID");

            if (gridViewRow.Cells[7].Text == "Reject" 
            || gridViewRow.Cells[7].Text == "Waiting")
            {
                 enableDelete =true;                                                
            }
            else
                checkBox.Enabled = false;
        }
        return enableDelete;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void btnDelete_Click1(object sender, EventArgs e)
    {
        string jobID = "";
        foreach (GridViewRow gridViewRow in GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkJobID");
            if (checkBox.Checked &&  
            (gridViewRow.Cells[7].Text == "Reject"
            || gridViewRow.Cells[7].Text == "Waiting"))
            {
                try
                {
                    jobID +=
                    gridViewRow.Cells[1].Text + ",";
                }
                catch (Exception ex)
                {
                    Literal1.Text = ex.Message;
                }
            }

        }
        if (jobID != "")
        {
            jobID = jobID.Substring(0, jobID.Length - 1);
            SqlServerProvider sqlServerProvider
                        = new SqlServerProvider();
            foreach (string job in jobID.Split(','))
                sqlServerProvider.DeleteAppliedJob(
                Convert.ToString(Session["jsuserid"]), job);
            ShowData();
        }
    }
}
